//
//  SceneDelegate.m
//  CustomFramework
//
//  Created by Houge on 2021/3/1.
//

#import "SceneDelegate.h"
#import "BaseTabBarController.h"
#import "BaseNavigationController.h"
#import "BaseWebViewController.h"

@interface SceneDelegate ()

@end

@implementation SceneDelegate


- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions  API_AVAILABLE(ios(13.0)){
    // 设置根控制器
    if (scene) {
        UIWindowScene *windowScence = (UIWindowScene *)scene;
        self.window = [[UIWindow alloc] initWithWindowScene:windowScence];
        self.window.frame = windowScence.coordinateSpace.bounds;
        BaseTabBarController *rootController = [[BaseTabBarController alloc] init];
        self.window.rootViewController = rootController;
        [self.window makeKeyAndVisible];
        
        // 三方库没适配,现在iOS13以后不显示广告
//        [self skipToAdLaunchView];
    }
}

#pragma mark - AD广告页
/// 跳转到广告页
- (void)skipToAdLaunchView {
    [XHLaunchAd setLaunchSourceType:SourceTypeLaunchScreen];
    //配置广告数据
    XHLaunchImageAdConfiguration *imageAdconfiguration = [[XHLaunchImageAdConfiguration alloc] init];
    imageAdconfiguration.duration = 3;
    imageAdconfiguration.frame = self.window.bounds;
    //广告图片URLString/或本地图片名(.jpg/.gif请带上后缀)
//    imageAdconfiguration.imageNameOrURLString = @"https://gitee.com/zhunjiee/picture-bed/raw/master/iOS/ad_image.png";
    // 注：加载本地图片不要放在Assets中
    imageAdconfiguration.imageNameOrURLString = @"ad_image.png";
    //为了展示效果更好,可设置为XHLaunchAdImageCacheInBackground,先缓存,下次显示
    imageAdconfiguration.imageOption = XHLaunchAdImageCacheInBackground;
    imageAdconfiguration.contentMode = UIViewContentModeScaleToFill;
    //广告点击打开页面参数(openModel可为NSString,模型,字典等任意类型)
    imageAdconfiguration.openModel = @"http://www.baidu.com";
    imageAdconfiguration.showFinishAnimate =ShowFinishAnimateLite;
    imageAdconfiguration.showFinishAnimateTime = 0.2;
    imageAdconfiguration.skipButtonType = SkipTypeTimeText;
    //显示开屏广告
    [XHLaunchAd imageAdWithImageAdConfiguration:imageAdconfiguration delegate:self];
}

/// 广告点击事件代理方法
- (BOOL)xhLaunchAd:(XHLaunchAd *)launchAd clickAtOpenModel:(id)openModel clickPoint:(CGPoint)clickPoint {
    /** openModel即配置广告数据设置的点击广告时打开页面参数(configuration.openModel) */
    if(openModel == nil) return NO;
    
    NSString *urlString = (NSString *)openModel;
    //此处跳转页面
    BaseWebViewController *webVC = [[BaseWebViewController alloc] init];
    webVC.urlStr = urlString;
    webVC.title = @"广告";
    //此处不要直接取keyWindow
    BaseTabBarController *rootVC = (BaseTabBarController *)[[UIApplication sharedApplication].delegate window].rootViewController;
    BaseNavigationController *nav = rootVC.viewControllers.firstObject;
    [nav pushViewController:webVC animated:YES];
    return YES; //YES移除广告,NO不移除广告
}


- (void)sceneDidDisconnect:(UIScene *)scene  API_AVAILABLE(ios(13.0)){
    // Called as the scene is being released by the system.
    // This occurs shortly after the scene enters the background, or when its session is discarded.
    // Release any resources associated with this scene that can be re-created the next time the scene connects.
    // The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
}


- (void)sceneDidBecomeActive:(UIScene *)scene  API_AVAILABLE(ios(13.0)){
    // Called when the scene has moved from an inactive state to an active state.
    // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}


- (void)sceneWillResignActive:(UIScene *)scene  API_AVAILABLE(ios(13.0)){
    // Called when the scene will move from an active state to an inactive state.
    // This may occur due to temporary interruptions (ex. an incoming phone call).
}


- (void)sceneWillEnterForeground:(UIScene *)scene  API_AVAILABLE(ios(13.0)){
    // Called as the scene transitions from the background to the foreground.
    // Use this method to undo the changes made on entering the background.
}


- (void)sceneDidEnterBackground:(UIScene *)scene  API_AVAILABLE(ios(13.0)){
    // Called as the scene transitions from the foreground to the background.
    // Use this method to save data, release shared resources, and store enough scene-specific state information
    // to restore the scene back to its current state.
}


@end
